<?php

namespace App\Http\Controllers\Admin;

use App\Exports\BondReturnExport;
use App\Exports\CashOutExport;
use App\Http\Controllers\Controller;
use App\Models\BondReturnLogs;
use App\Models\Mails;
//use App\Models\MailTemplates;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class BondReturnLogsController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(Request $request){
        $params = [
            'page' => 1,
            'kwd' => '',
            'status' => 0,
            'start' => '',
            'end' => '',
        ];
        //如果从代办点击过来，则显示待审核
        $from = $request->get('from',0);
        if ($from == 1){
            $params['status'] = 1;
            $from = 0;
        }
        $params = json_encode($params);
        return view('admin.user.bond_return_logs',compact('params'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'status' => (int)$request->get('status',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
            ];
            $lists = BondReturnLogs::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 查看
     * @param Request $request
     * @return bool|view
     */
    public function edit(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            if($id){
                $info = BondReturnLogs::findOrFail($id);
            }
            $info = json_encode($info);

            return view('admin.user.bond_return_logs_detail',compact('info','id'));
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 审核
     * @param Request $request
     * @return bool
     */
    public function active(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = BondReturnLogs::findOrFail($id);

            //发送站内信
            $msg = BondReturnLogs::where('id',$id)->first()->toArray();//保证金退还信息
            //消息入库
            $template = array();
            $template['user_id'] = $msg['user_id'];
            $template['title'] = '保证金退还';
            $template['content'] = '您申请的保证金退还已成功，金额:'.$msg['bond'];
            $template['status'] = 1;
            $template['type'] = 1;
            Mails::create($template);

            $info->update(['status' => BondReturnLogs::STATUS_2 , 'return_name' => auth()->guard('admin')->user()->username , 'return_id' => auth()->guard('admin')->user()->id , 'return_at' => date('Y-m-d H:i:s')]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!is_array($id)){
                $id=[$id];
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            BondReturnLogs::whereIn('id',$id)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     *  Excel导出
     * @param Request $request
     * @return bool
     */
    public function exports(Request $request)
    {
        $ids = $request->get('ids');
        $export = new BondReturnExport($ids);
        //这里可以传参到服务层
        return Excel::download($export, '保证金退还表'.date("Y-m-d H:i:s").'.xlsx');
    }

    /**
     * 通知消息 内容转换函数
     *
     * @param string $message 内容模板
     * @param array $param 内容参数数组
     * @return string 通知内容
     */
    function ds_replace_text($message, $param)
    {
        if (!is_array($param))
            return false;
        foreach ($param as $k => $v) {
            $message = str_replace('{$' . $k . '}', $v, $message);
        }
        return $message;
    }
}
